﻿/*
	Camera Controller - In charge of setting up camera if needed, and updating it's position.
*/

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

	public static CameraController instance;
	public Transform target;

	public float height = 5.0f;
	public float heightDamping = 5.0f;
	public float distance = 10.0f;

	void Awake(){
		instance = this;
	}

	void LateUpdate () {
		if(!target)
			return;

		float nextHeight = target.position.y + height;

		float currentHeight = transform.position.y;
		currentHeight = Mathf.Lerp(currentHeight, nextHeight, heightDamping * Time.deltaTime);

		transform.position = new Vector3(target.position.x, currentHeight, target.position.z - distance);

		transform.LookAt(target);	
	}

	public static void Setup(){
		GameObject tempCam;
		GameObject tempTarget;
		CameraController cc;

		if(Camera.main != null){
			tempCam = Camera.main.gameObject;
		}else{
			tempCam = new GameObject("MainCamera");
			tempCam.AddComponent("Camera");
			tempCam.tag = "MainCamera";			
		}

		tempCam.AddComponent("CameraController");
		cc = tempCam.GetComponent("CameraController") as CameraController;

		tempTarget = GameObject.Find("Player") as GameObject;

		cc.target = tempTarget.transform;
		if(cc.target == null){
			//? Can assume there would always be a Player object, but if not?
		}
	}
}
